package com.gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JLabel;

import com.dao.UsersDao;
import com.metier.UsersMetier;
import com.tools.BICButtonDegrade;
import com.tools.BICStyle;

/**
 * 
 * @author Sarr_moussa
 */
public class FenLogin extends javax.swing.JFrame implements KeyListener {
	// Variables declaration - do not modify//GEN-BEGIN:variables
	private UsersMetier loginMetier;
	private javax.swing.JButton boutonValider;
	private javax.swing.JButton boutonAnnuler;
	private javax.swing.JLabel jLabel1;
	private javax.swing.JLabel jLabel3;
	private javax.swing.JLabel jLabel4;

	private javax.swing.JLabel labelConnecter;
	private javax.swing.JLabel jLabel6;
	private javax.swing.JPanel jPanel1;
	private javax.swing.JPanel jPanel2;
	private javax.swing.JPanel jPanel3;
	private javax.swing.JTextField textLogin;
	private javax.swing.JPasswordField textPassword;
	private String Login;

	private javax.swing.JLabel labelErreurAuthentification;

	// End of variables declaration

	public javax.swing.JTextField getTextLogin() {
		return textLogin;
	}

	public void setTextLogin(javax.swing.JTextField textLogin) {
		this.textLogin = textLogin;
	}

	public javax.swing.JPasswordField getTextPassword() {
		return textPassword;
	}

	public void setTextPassword(javax.swing.JPasswordField textPassword) {
		this.textPassword = textPassword;
	}

	/**
	 * Creation de la Fenetre de Login nommée FenLogin
	 */
	public FenLogin() {
		loginMetier = new UsersMetier();
		initComponents();
		setTitle("Authentification");
		setLocationRelativeTo(this);
		getContentPane().setBackground(new java.awt.Color(128, 130, 133));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		go();
	}

	private void initComponents() {

		jPanel1 = new javax.swing.JPanel();
		 labelConnecter = new javax.swing.JLabel();
		jPanel3 = new javax.swing.JPanel();
		textLogin = new javax.swing.JTextField();

		textPassword = new javax.swing.JPasswordField();
		jLabel3 = new javax.swing.JLabel();
		jLabel4 = new javax.swing.JLabel();
		jLabel6 = new javax.swing.JLabel();
		jLabel1 = new javax.swing.JLabel();
		labelErreurAuthentification = new javax.swing.JLabel();
		jPanel2 = new javax.swing.JPanel();
		boutonAnnuler = new javax.swing.JButton();

		boutonValider = new javax.swing.JButton();

		setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

		textLogin.addKeyListener(this);
		textPassword.addKeyListener(this);

		setTitle("Authentification");
		setFont(new java.awt.Font("Verdana", 3, 14)); // NOI18N
		setResizable(false);                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         

		jPanel1.setBackground(new java.awt.Color(191, 36, 94));
		jPanel1.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1,
				1, new java.awt.Color(191, 36, 94)));
		labelConnecter = new JLabel(
				"		"
						+ "VEUILLEZ - VOUS IDENTIFIER POUR ACCEDER A VOTRE ESPACE DE TRAVAIL PERSONNEL S'IL VOUS PLAIT !!!"
						+ "    ");
		labelConnecter.setBorder(null);
		labelConnecter.setFont(new Font("Arial", Font.ITALIC, 15));
		labelConnecter.setForeground(BICStyle.getColorWhite());

		javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(
				jPanel1);
		jPanel1.setLayout(jPanel1Layout);
		jPanel1Layout.setHorizontalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel1Layout
						.createSequentialGroup()
						.addContainerGap()
						.addComponent(labelConnecter,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE).addContainerGap()));
		jPanel1Layout.setVerticalGroup(jPanel1Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				jPanel1Layout
						.createSequentialGroup()
						.addContainerGap()
						.addComponent(labelConnecter,
								javax.swing.GroupLayout.DEFAULT_SIZE, 37,
								Short.MAX_VALUE).addContainerGap()));

		jPanel3.setBackground(new java.awt.Color(88, 89, 91));
		jPanel3.setBorder(javax.swing.BorderFactory
				.createLineBorder(new java.awt.Color(191, 36, 94)));

		textLogin.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {

			}
		});

		textPassword.addActionListener(new java.awt.event.ActionListener() {
			public void actionPerformed(java.awt.event.ActionEvent evt) {

			}
		});

		jLabel3.setIcon(new javax.swing.ImageIcon("src/main/java/com/photos/Autentification.jpg")); // NOI18N
		jLabel3.setToolTipText("Authentification");
		jLabel3.setBorder(javax.swing.BorderFactory
				.createLineBorder(new java.awt.Color(191, 36, 94)));

		jLabel4.setIcon(new javax.swing.ImageIcon("src/main/java/com/photos/index.jpg")); // NOI18N
		jLabel4.setToolTipText(Login);
		jLabel4.setBorder(javax.swing.BorderFactory
				.createLineBorder(new java.awt.Color(191, 36, 94)));

		jLabel6.setIcon(new javax.swing.ImageIcon("src/main/java/com/photos/password.jpg")); // NOI18N
		jLabel6.setBorder(javax.swing.BorderFactory
				.createLineBorder(new java.awt.Color(191, 36, 94)));

		javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(
				jPanel3);
		jPanel3.setLayout(jPanel3Layout);
		jPanel3Layout
				.setHorizontalGroup(jPanel3Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								jPanel3Layout
										.createSequentialGroup()
										.addContainerGap()
										.addGroup(
												jPanel3Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(jLabel6)
														.addComponent(jLabel4))
										.addPreferredGap(
												javax.swing.LayoutStyle.ComponentPlacement.RELATED,
												30, Short.MAX_VALUE)
										.addGroup(
												jPanel3Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING,
																false)
														.addComponent(
																textPassword,
																javax.swing.GroupLayout.DEFAULT_SIZE,
																206,
																Short.MAX_VALUE)
														.addComponent(textLogin))
										.addGap(18, 18, 18)
										.addComponent(jLabel3)
										.addGap(15, 15, 15)));
		jPanel3Layout
				.setVerticalGroup(jPanel3Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								jPanel3Layout
										.createSequentialGroup()
										.addContainerGap(37, Short.MAX_VALUE)
										.addGroup(
												jPanel3Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.LEADING)
														.addComponent(
																jLabel3,
																javax.swing.GroupLayout.Alignment.TRAILING)
														.addGroup(
																javax.swing.GroupLayout.Alignment.TRAILING,
																jPanel3Layout
																		.createSequentialGroup()
																		.addGroup(
																				jPanel3Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.TRAILING)
																						.addComponent(
																								jLabel4)
																						.addComponent(
																								textLogin,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								46,
																								javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addGap(34,
																				34,
																				34)
																		.addGroup(
																				jPanel3Layout
																						.createParallelGroup(
																								javax.swing.GroupLayout.Alignment.BASELINE)
																						.addComponent(
																								textPassword,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								46,
																								javax.swing.GroupLayout.PREFERRED_SIZE)
																						.addComponent(
																								jLabel6,
																								javax.swing.GroupLayout.PREFERRED_SIZE,
																								50,
																								javax.swing.GroupLayout.PREFERRED_SIZE))
																		.addGap(50,
																				50,
																				50)))));

		jLabel1.setBackground(new java.awt.Color(255, 204, 255));
		jLabel1.setIcon(new javax.swing.ImageIcon("src/main/java/com/photos/Logo Groupe IDyal.jpg")); // NOI18N
		jLabel1.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1,
				1, new java.awt.Color(191, 36, 94)));

		jPanel2.setBackground(new java.awt.Color(128, 130, 133));
		jPanel2.setBorder(javax.swing.BorderFactory.createMatteBorder(1, 1, 1,
				1, new java.awt.Color(191, 36, 94)));
		
		boutonAnnuler = new BICButtonDegrade("ANNULER");
		boutonAnnuler.setToolTipText("Effacer les champs ");
		textLogin.setToolTipText("Mettez ici votre login ");
		textPassword.setToolTipText("Mettez ici votre mot de passe");
		textLogin.setFont(new Font("Arial", Font.BOLD, 28));
		textPassword.setFont(new Font("Arial", Font.BOLD, 28));

		boutonValider = new BICButtonDegrade("VALIDER");
		boutonValider.setToolTipText("Valider la saisie pour se connecter");

		boutonValider.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent even) {

				loginMetier.testConnect(textLogin.getText(),
						textPassword.getText());
				// loginMetier.recupereProfil(textLogin.getText());
				UsersDao userDao = new UsersDao();
				boolean conectExistUser = userDao.connect(textLogin.getText(),
						textPassword.getText());
				if (conectExistUser == true)
					dispose();

			}
		});
		boutonAnnuler.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				textLogin.setText("");
				textPassword.setText("");

			}
		});

		javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(
				jPanel2);
		jPanel2.setLayout(jPanel2Layout);
		jPanel2Layout.setHorizontalGroup(jPanel2Layout.createParallelGroup(
				javax.swing.GroupLayout.Alignment.LEADING).addGroup(
				javax.swing.GroupLayout.Alignment.TRAILING,
				jPanel2Layout
						.createSequentialGroup()
						.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE,
								Short.MAX_VALUE)
						.addComponent(boutonValider,
								javax.swing.GroupLayout.PREFERRED_SIZE, 141,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(135, 135, 135)
						.addComponent(boutonAnnuler,
								javax.swing.GroupLayout.PREFERRED_SIZE, 141,
								javax.swing.GroupLayout.PREFERRED_SIZE)
						.addGap(168, 168, 168)));
		jPanel2Layout
				.setVerticalGroup(jPanel2Layout
						.createParallelGroup(
								javax.swing.GroupLayout.Alignment.LEADING)
						.addGroup(
								javax.swing.GroupLayout.Alignment.TRAILING,
								jPanel2Layout
										.createSequentialGroup()
										.addContainerGap(
												javax.swing.GroupLayout.DEFAULT_SIZE,
												Short.MAX_VALUE)
										.addGroup(
												jPanel2Layout
														.createParallelGroup(
																javax.swing.GroupLayout.Alignment.BASELINE)
														.addComponent(
																boutonValider,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																44,
																javax.swing.GroupLayout.PREFERRED_SIZE)
														.addComponent(
																boutonAnnuler,
																javax.swing.GroupLayout.PREFERRED_SIZE,
																44,
																javax.swing.GroupLayout.PREFERRED_SIZE))
										.addContainerGap()));

		javax.swing.GroupLayout layout = new javax.swing.GroupLayout(
				getContentPane());
		getContentPane().setLayout(layout);
		layout.setHorizontalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE,
						javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
				.addComponent(jPanel2,
						javax.swing.GroupLayout.Alignment.TRAILING,
						javax.swing.GroupLayout.DEFAULT_SIZE,
						javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
				.addGroup(
						javax.swing.GroupLayout.Alignment.TRAILING,
						layout.createSequentialGroup()
								.addComponent(jLabel1,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										255, Short.MAX_VALUE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jPanel3,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)));
		layout.setVerticalGroup(layout
				.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
				.addGroup(
						layout.createSequentialGroup()
								.addComponent(jPanel1,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										layout.createParallelGroup(
												javax.swing.GroupLayout.Alignment.LEADING,
												false)
												.addComponent(
														jPanel3,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														javax.swing.GroupLayout.DEFAULT_SIZE,
														Short.MAX_VALUE)
												.addComponent(
														jLabel1,
														javax.swing.GroupLayout.PREFERRED_SIZE,
														0, Short.MAX_VALUE))
								.addPreferredGap(
										javax.swing.LayoutStyle.ComponentPlacement.RELATED)
								.addComponent(jPanel2,
										javax.swing.GroupLayout.PREFERRED_SIZE,
										javax.swing.GroupLayout.DEFAULT_SIZE,
										javax.swing.GroupLayout.PREFERRED_SIZE)
								.addContainerGap(28, Short.MAX_VALUE)));

		pack();
	}

	/**
	 * @param args
	 *            the command line arguments
	 */
	public static void main(String args[]) {

		try {
			for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager
					.getInstalledLookAndFeels()) {
				if ("Nimbus".equals(info.getName())) {
					javax.swing.UIManager.setLookAndFeel(info.getClassName());
					break;
				}
			}
		} catch (ClassNotFoundException ex) {
			java.util.logging.Logger.getLogger(FenLogin.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (InstantiationException ex) {
			java.util.logging.Logger.getLogger(FenLogin.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (IllegalAccessException ex) {
			java.util.logging.Logger.getLogger(FenLogin.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		} catch (javax.swing.UnsupportedLookAndFeelException ex) {
			java.util.logging.Logger.getLogger(FenLogin.class.getName()).log(
					java.util.logging.Level.SEVERE, null, ex);
		}

		/*
		 * Create and display the form
		 */
		java.awt.EventQueue.invokeLater(new Runnable() {

			public void run() {
				new FenLogin().setVisible(true);
			}
		});
	}

	public void keyPressed(KeyEvent e) {
		// TODO Auto-generated method stub
		if (e.getKeyCode() == KeyEvent.VK_ENTER) {
			loginMetier
					.testConnect(textLogin.getText(), textPassword.getText());
			// loginMetier.recupereProfil(textLogin.getText());
			UsersDao userDao = new UsersDao();
			boolean conectExistUser = userDao.connect(textLogin.getText(),
					textPassword.getText());
			if (conectExistUser == true)
				dispose();
		}

	}

	public void keyReleased(KeyEvent arg0) {
		// TODO Auto-generated method stub

	}

	public void keyTyped(KeyEvent arg0) {
		// TODO Auto-generated method stub
	}

	// la méthode qui permet au défilement du labelConnecter
	public void go() {
		long temps = 500;
		long startTime = 0;
		Timer timer = new Timer();
		TimerTask tache = new TimerTask() {
			public void run() {
				char chr = ' ';
				String text = null;
				chr = labelConnecter.getText().charAt(0);
				text = labelConnecter.getText().substring(1);
				text += chr;
				labelConnecter.setText(text);
			}
		};
		timer.scheduleAtFixedRate(tache, startTime, temps);
	}

}
